﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using skags.Models;

/*
* University of Reykjavík
* Brjóstagjöf.is is the product of first-year practical programming course
* for computer science students at said university
*
* URL: http://code.google.com/p/scags
* Authors: 
* Ástvaldur Sigurðsson
* Guðjón Jónsson
* Kristín Inga Vigfúsdóttir
* Steinar Bragi Sigurðsson
* Sveinn Ernstsson
*/

namespace skags.Controllers
{
    [HandleError]
    [LogError]
    public class ArticleController : Controller
    {
        private IDataRepository m_rep;

        /* Notum þennan smið fyrir unit testing */
        public ArticleController()
        {
            m_rep = new ArticleRepository();
        }

        /* Notum þennan smið fyrir unit testing */
        public ArticleController(IDataRepository iRep)
        {
            m_rep = iRep;
        }

        /* GET: Ná í alla flokka og greina */ 
        public ActionResult Index(int id = 30)
        {
            var allArticlesAndCat = m_rep.GetAll(id);

            return View(allArticlesAndCat);
        }

        /* GET: /Article/ViewItem/5 (skoða tiltekna grein) */
        public ActionResult ViewItem(int id)
        {
            var urep = new UserRepository();

            var articleItem = m_rep.GetArticleByID(id);
            if (articleItem == null)
                throw (new Exception("Grein fannst ekki"));

            m_rep.UpdateReadCount(articleItem);

            // Notum user repository til að fá user id og nafn
            ViewData["CreatorName"] = urep.GetNameByHashID(articleItem.CreatorID);
            ViewData["CreatorID"] = urep.GetIDByHashID(articleItem.CreatorID);

            return View(articleItem);
        }

        /* GET: /Article/Category/5 (skoðar tiltekinn flokk) */
        public ActionResult Category(int id)
        {
            var allArticlesAndCat = m_rep.GetArticlesByCategoryID(id);
            ViewData["exp"] = allArticlesAndCat;

            return View(allArticlesAndCat);
        }
    }
}